<template>
    <div id="box">
        <!--         登录表单-->
        <div id="register_container">
            <h1>欢迎注册</h1>
            <el-form label-position="left" ref="registerForm" label-width="80px" class="register_form" :model="registerForm" :rules="rules">
                <el-form-item prop="username">
                    <el-input v-model="registerForm.username" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input type="password" v-model="registerForm.password" placeholder="请输入密码"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">立即注册</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import instance from "@/http";
export default {
    name: "RegisterView",
    data() {
        return {
            registerForm: {
                username: '',
                password: ''
            },
            rules: {
                username: [
                    // required  必填项  message: 提示信息  trigger: 校验的触发时机  失去焦点时
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    // min: 最小长度   max 最大长度
                    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 6, max: 12, message: '长度在 6 到 12 个字符', trigger: 'blur' }
                ]
            }
        }
    },
    methods:{
        onSubmit(){
            this.$refs.registerForm.validate((valid) => {
                //  表单校验通过  true   否则 false
                if (valid) {
                    //  发送http请求    Axios 实现http请求的发送   参数2  标识发送http请求时 需要携带的参数
                    instance.post("/user/register",this.registerForm)
                        .then(res=>{   //  请求成功后的回调   res响应对象
                            if (res.data.code==0){
                                this.$notify.success("注册成功")
                                // 跳转路由到登录页
                                this.$router.push({name:'login'})
                            }

                        })
                } else {
                    this.$notify.warning("请填写必要数据")
                    return false;
                }
            });
        }
    }
}
</script>

<style scoped>
#box{
    height: 100vh;
    background-color: rgb(26, 160, 148);
}

#register_container{

    width: 450px;
    height: 380px;
    background-color: #fff;
    border-radius: 30px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0 0 15px #fff;
}

.register_form{
    position: absolute;
    bottom: 70px;
    right: 35px;
    border-bottom: 0;
    box-sizing: border-box;
    width: 100%;
    padding: 0 20px;
}
h1{
    position: absolute;
    top: 40px;
    left: 150px;
    z-index: 999;
    color: #67C23A;
}
button{
    width: 100%;
}
</style>